package com.cookman.gameobjects;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.cookman.gameobjects.decks.DiseaseDeckActor;
import com.cookman.serviceLocators.ServiceLocator;
import com.cookman.utils.AssetUtils;
import com.cookman.world.entities.Disease;

/**
 * Created by Cookman on 16.09.14.
 */
public class DiseaseCardActor extends AbstractGameObject {

    protected float scale = 0.8f;
    final protected Disease disease;
    private Boolean isSelected = false;

    public Disease getDisease() {
        return disease;
    }

    protected Sprite diseaseIcon;

    private DiseaseDeckActor parentDeck;

    public DiseaseCardActor(Disease disease, DiseaseDeckActor parentDeck) {
        super();
        this.disease = disease;
        this.parentDeck = parentDeck;
        diseaseIcon = AssetUtils.getDiseaseByColor(disease.getColor());
      //  setBounds(0, 0, scale, scale);
        addListener(mouseActorListener);
        setTouchable(Touchable.enabled);
    }

    public void setIsSelected(Boolean isSelected) {
        this.isSelected = isSelected;
    }

    public Boolean getIsSelected() {
        return isSelected;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        if (isSelected) {
            batch.draw(diseaseIcon, getX(), getY(), getScaleX()* 1.05f, getScaleY() * 1.05f);
        } else {
            batch.draw(diseaseIcon, getX(), getY(), getScaleX(), getScaleY());
        }
    }

    @Override
    public void renderGUI(SpriteBatch batch) {
    }

    @Override
    public void renderShape(ShapeRenderer renderer) {
    }

    private static InputListener mouseActorListener = new InputListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y,
                                 int pointer, int button) {
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer,
                            int button) {
            DiseaseCardActor actor = (DiseaseCardActor) event.getTarget();
            System.out.println("Disease card touched " + actor.getDisease().getColor());
            actor.parentDeck.resetSelection();
            actor.setIsSelected(true);
            ServiceLocator.getUIController().selectDisease(actor.getDisease());
        }

        @Override
        public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            super.enter(event, x, y, pointer, fromActor);
            DiseaseCardActor actor = (DiseaseCardActor) event.getTarget();
        }
    };
}
